import router from '@/router'
import store from '@/store'
const whitePath = ['/login']
router.beforeEach(async (to, from, next) => {
  // 如果token存在,不让进login
  if (store.getters.token) {
    if (to.path === '/login') {
      next('/')
    } else {
      if (!store.getters.hasUserInfo) {
        const { permission } = await store.dispatch('user/getUserProfile')
        const filterRoutes = await store.dispatch(
          'permission/setRoutes',
          permission.menus
        )
        filterRoutes.forEach((item) => {
          router.addRoute(item)
        })
        // 添加完动态路由之后，需要在进行一次主动跳转
        return next(to.path)
      }
      next()
    }
  } else {
    // 否则,让进login
    if (whitePath.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})
